# -*- coding: utf-8 -*-
"""
Created on Fri Jun  2 16:57:47 2023

@author: mi

optimization 优化函数的实现 求导
"""

import numpy as np
from dezero import Variable
from dezero.utils import plot_dot_graph

def sphere(x,y):
    """sphere函数"""
    z = x**2 + y**2
    return z

def matyas(x,y):
    """matyas函数"""
    z = 0.26 * (x**2 + y**2) - 0.48*x*y
    return z

def goldstein(x,y):
    """GOLDSTEIN-PRICE函数"""
    z = (1+(x+y+1)**2 * (19- 14*x +3*x**2 - 14*y + 6*x*y + 3*y**2))*\
        (30+(2*x - 3*y)**2 * (18 - 32*x + 12*x**2 + 48*y - 36*x*y + 27*y**2))
    return z

x = Variable(np.array(1.0))
y = Variable(np.array(1.0))
z = goldstein(x, y)
z.backward()
print(x.grad, y.grad)

x.name = 'x'
y.name = 'y'
z.name = 'z'
plot_dot_graph(z,to_file='goldstein.png')